import 'package:flutter/cupertino.dart';
import 'package:flutter_base/config/app_routes.dart';
import 'package:flutter_base/utils/log/utils_log.dart';
import 'package:get/get.dart';
import 'package:flutter_base/base/controller/base_controller.dart';
import 'package:flutter_base/utils/loading/loading_utils.dart';
import 'package:flutter_base/http/http_export.dart';
import 'package:flutter_base/http/http_manager.dart';
import 'package:flutter_base/http/api.dart';
import 'package:flutter_base/config/app_config.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'model/model_test_psychology.dart';

class HomeLogic extends BaseController {
  List<Map<String, dynamic>> bannerList = [];
  List<Map<String, dynamic>> productList = [];
  List<Map<String, dynamic>> articleList = [];

  // 受欢迎的心理测试列表
  List<ModelTestPsychology> popularTests = [];

  // 推荐的心理测试列表
  List<ModelTestPsychology> recommendTests = [];

  // 刷新控制器
  RefreshController refreshController = RefreshController();

  // 分页参数
  int page = 1;
  int pageSize = 20;

  ScrollController scrollController = ScrollController();
  RxDouble scrollPixels = 0.0.obs;

  @override
  void onReady() {
    super.onReady();
    loadRecommendData();
    loadPopularTests();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    scrollController.addListener(scrollListener);
  }

  scrollListener() {
    scrollPixels.value = scrollController.position.pixels;
// clog(scrollPixels.value);
  }

  @override
  void onClose() {
    refreshController.dispose();
    scrollController.removeListener(scrollListener);
    scrollController.dispose();
    super.onClose();
  }

  // 加载首页数据
  Future<void> loadRecommendData() async {
    HttpResponse response = await HttpManager().get(
      API.popularTests,
      params: {
        'page': page,
        'size': pageSize,
        'rec': true,
      },
      fromJson: ModelTestPsychology.fromJson,
      uiType: HttpUIType.toast,
    );
    if (response.isSuccess) {
      recommendTests = response.data;
      update();
    }
  }

  // 加载受欢迎的心理测试
  Future<void> loadPopularTests({bool refresh = true}) async {
    if (refresh) {
      page = 1;
    }
    HttpResponse response = await HttpManager().get(
      API.popularTests,
      params: {
        'page': page,
        'size': pageSize,
        'popular': true,
      },
      fromJson: ModelTestPsychology.fromJson,
      uiType: refresh ? HttpUIType.toast : HttpUIType.none,
    );
    if (response.isSuccess) {
      if (refresh) {
        popularTests = response.data;
      } else {
        popularTests.addAll(response.data);
      }

      bool hasMore = response.data.length == 20;
      // 更新刷新控制器状态
      if (refresh) {
        // refreshController.com

        refreshController.refreshCompleted();
        if (!hasMore) {
          refreshController.resetNoData();
          refreshController.loadNoData();
        }
      } else {
        if (hasMore) {
          // refreshController.refreshCompleted();
          refreshController.loadComplete();
        } else {
          refreshController.loadNoData();
        }
      }

      // doFinished();
    } else {
      if (refresh) {
        refreshController.refreshFailed();
      } else {
        refreshController.loadFailed();
      }
    }
  }

  // 点击搜索
  void onSearchTap() {
    // 处理搜索框点击
    Get.toNamed(AppRoutes.search);

    // print('点击搜索');
  }

  // 点击立即对话
  void onTalkNowTap() {
    // 跳转到数字人对话页面
    Get.toNamed(AppRoutes.digitalHumanChat);
  }

  // 点击推荐卡片
  void onRecommendCardTap(int index, String title) {
    LoadingUtils.showToast('点击了$title');
    // 这里可以实现跳转到对应功能页面的逻辑
  }
}
